﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Drifting.Foundation;

namespace Drifting.MotionPlanning.Systems.CollisionPrimitives
{
    abstract public class Polygon
    {
        protected List<double[]> vertices;
        protected double[] coordinates;

        public Polygon() { }

        public Polygon(string rawFile)
        {
            string[] separators = new string[] { "<vertex>", "</vertex>" };
            string[] entries = rawFile.Split(separators, StringSplitOptions.None);
            vertices = new List<double[]>();
            int n_vertices = (entries.Length - 1) / 2;
            for (int i = 0; i < n_vertices; ++i)
                vertices.Add(TextAssistant.StringToDoubleArray(entries[1 + i * 2]));

            separators = new string[] { "<position>", "</position>" };
            entries = rawFile.Split(separators, StringSplitOptions.None);
            this.coordinates = TextAssistant.StringToDoubleArray(entries[1]);
        }

        public double X
        {
            get { return coordinates[0]; }
            set { coordinates[0] = value; }
        }

        public double Y
        {
            get { return coordinates[1]; }
            set { coordinates[1] = value; }
        }

        public double Orientation
        {
            get { return coordinates[2]; }
            set { coordinates[2] = value; }
        }
        
        abstract public bool IsCollidedWithIfAt(double[] coord, Polygon targetPolygon);

        abstract public void TransformToEnvironment(double[] coord, ref List<double[]> vertices_real);

        public List<double[]> Vertices
        {
            get{ return vertices; }
            set{ this.vertices = value; }
        }

        public double[] Coordinates
        {
            get { return coordinates; }
            set { this.coordinates = value; }
        }
    }
}
